package com.travel.server.manager.impl.command.project.data;

import com.travel.common.util.CommonUtil;
import com.travel.server.manager.dto.request.project.data.UpdateProjectCostDetailRequest;
import com.travel.server.manager.dto.response.EmptyResponse;
import com.travel.server.manager.impl.command.AbstractCommand;
import com.travel.server.manager.impl.dal.SpecialDal;
import com.travel.server.manager.model.ProjectSpecial;
import com.travel.server.manager.model.UserInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @Author: LiZhenhui
 * @Date: 2018/11/29 12:13
 * @Desc:
 */
@Component
@Slf4j
public class UpdateProjectCostDetailCommand extends AbstractCommand<UpdateProjectCostDetailRequest, EmptyResponse> {

    @Autowired
    private SpecialDal specialDal;

    @Override
    protected void checkInput(UpdateProjectCostDetailRequest request) {
        if(request.getId() == null){
            throwInvalidParam();
        }
    }

    @Override
    public EmptyResponse handle(UpdateProjectCostDetailRequest request) {
        checkInput(request);
        UserInfo userInfo = getUserInfo();
        Date now = new Date();
        ProjectSpecial special = specialDal.selectById(request.getId());
        special.setCompensate(CommonUtil.yuanToFen(request.getCompensate()).intValue());
        special.setInsurance(CommonUtil.yuanToFen(request.getInsurance()).intValue());
        special.setMaterial(CommonUtil.yuanToFen(request.getMaterial()).intValue());
        special.setPartner(CommonUtil.yuanToFen(request.getPartner()).intValue());
        special.setRemark(request.getRemark());
        special.setTaxation(CommonUtil.yuanToFen(request.getTaxation()).intValue());
        special.setTravel(CommonUtil.yuanToFen(request.getTravel()).intValue());
        special.setUpdateBy(userInfo.getName());
        special.setUpdateTime(now);
        specialDal.insertOrUpdate(special);
        return new EmptyResponse();
    }
}
